﻿using LY.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LY.Interface;
using QJ.CMS.Filters;

using LY.Common.Utils;
using LY.Entity.SearchEntity;
using LY.Entity.PageSearch;
using LY.Entity.Jsons;
using LY.Entity.Entities;
namespace QJ.CMS.Controllers
{
    [MyAuthorization]
    public class UserDetailsController : Controller
    {
        //
        // GET: /UserDetails/

        private readonly ISiteconfigService _siteconfigService;
        private IReferralsService _referrals;
        private IMessagesService _messages;
        private ILoginService _lservice;
        private IScoreService _scoreService;
        public UserDetailsController(ISiteconfigService siteconfigService, IReferralsService referrals, IMessagesService messages, ILoginService lService, IScoreService scoreService)
        {
            _siteconfigService = siteconfigService;
            _referrals = referrals;
            _messages = messages;
            _lservice = lService; 
            _scoreService = scoreService;
        }
        #region 我的邀请链接
        public ActionResult ReferralsLink()
        {
            var siteconfig = _siteconfigService.LoadConfig(); ;
            if (Session["CurrentUser"] != null)
            {
                var member = (U_Member)Session["CurrentUser"];
                //先将id加密
                string md5id = Base64Util.Encrypt(member.ID.ToString());
                ViewBag.Mid = Base64Util.Decrypt(md5id);
                //ViewBag.Url = siteconfig.Weburl + "?id=" + md5id;
                ViewBag.Url = md5id;
            }
            return View();
        }
        //列表
        [HttpPost]
        public ActionResult ReferralsList(ReferralsSearch search, FormCollection form)
        {
            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];

            PagedList<Member_EX> rList = _referrals.GetReferralsLink(search.MemberId, view);
            JsonQTable at = JsonQTable.ConvertFromPagedList<Member_EX>(rList, colkey, colsinfo.Split(','));
            return Json(at);
        }

        #endregion

        #region 我的消息
        public ActionResult SendMessage()
        {
            return View();
        }

        //我的消息列表
        public ActionResult authentication()
        {
            return View();
        }
        //消息列表
        [HttpPost]
        public ActionResult MessageList(FormCollection form)
        {

            var member = (U_Member)Session["CurrentUser"];

            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];
            PagedList<U_Messages_EX> bList = _messages.MyMessageLink(member.ID, view);
            JsonQTable at = JsonQTable.ConvertFromPagedList<U_Messages_EX>(bList, colkey, colsinfo.Split(','));
            return Json(at);
        }

        //私信详情
        [HttpPost]
        public JsonResult MessageDetail(string m_id, string r_id)
        {
            List<U_Messages_EX> mlist = _messages.MyMessageDetail(m_id, r_id);

            return Json(mlist, JsonRequestBehavior.AllowGet);
        }

        //回复私信
        [HttpPost]
        public JsonResult AddMesssages(int m_id, int r_id, string content)
        {
            JsonSMsg msg = new JsonSMsg();

            U_Messages m = new U_Messages();
            m.MemberId = r_id;
            m.MessageMemberId = m_id;
            m.Message = content;
            m.AddTime = DateTime.Now;
            m.Status = 0;

            int result = _messages.AddMessages(m);

            if (result > 0)
            {
                msg.Status = 1;
                msg.Data = Url.Action("authentication");
                msg.Message = "回复成功！";

            }
            else
            {
                msg.Status = -1;
                msg.Message = "回复失败！";
            }

            return Json(msg);
        }

        //标识已读
        [HttpPost]
        public ActionResult UpdateMessageStatus(string id)
        {
            JsonSMsg msg = new JsonSMsg();

            int result = _messages.UpdateMessageStatus(id);

            if (result > 0)
            {
                msg.Status = 1;
                msg.Data = Url.Action("authentication");
                msg.Message = "标识成功！";
            }
            else
            {
                msg.Status = -1;
                msg.Message = "标识失败！";
            }

            return Json(msg);
        }

        //发送私信
        [HttpPost]
        public ActionResult SendMessage(U_Messages_EX m)
        {
            JsonSMsg msg = new JsonSMsg();

            if (m.Code == Session["ValidateCode"].ToString())
            {
                int id = _messages.IsNameExists(m.Name);
                if (id > 0)
                {
                    if (id != (Session["CurrentUser"] as U_Member).ID)
                    {
                        m.MemberId = id;
                        m.MessageMemberId = (Session["CurrentUser"] as U_Member).ID;
                        m.AddTime = DateTime.Now;
                        m.Status = 0;
                        int result = _messages.AddMessages(m);

                        if (result > 0)
                        {
                            msg.Status = 1;
                            msg.Message = "私信发送成功！";
                            msg.Data = Url.Action("authentication");
                        }
                    }
                    else
                    {
                        msg.Status = -1;
                        msg.Message = "不可以给自己发私信哦！";
                    }
                }
                else
                {
                    msg.Status = -1;
                    msg.Message = "收件人不存在！";
                }
            }
            else
            {
                msg.Status = -1;
                msg.Message = "验证码错误！";
            }

            return Json(msg);
        }

        //删除私信
        [HttpPost]
        public ActionResult DeleteMessage(string mid, string rmid)
        {
            JsonSMsg msg = new JsonSMsg();

            int result = _messages.DeleteMessage(mid, rmid);

            if (result > 0)
            {
                msg.Status = 1;
                msg.Message = "删除成功！";
                msg.Data = Url.Action("authentication");
            }
            else
            {
                msg.Status = -1;
                msg.Message = "删除失败！";
            }

            return Json(msg);
        }
        #endregion
         
        #region 我的积分  
        public ActionResult MyScore()
        {
            var member = (U_Member)Session["CurrentUser"];
            //获取会员总积分 
            ViewBag.scoreCount = _scoreService.GetScoreByMID(member.ID);
            //获取会员详细积分列表
            var scoreList = _scoreService.GetScoreByMemeberId(member.ID);
            return View(scoreList);
        }
        [HttpPost]
        public ActionResult MyScore(ScoreSearch search, FormCollection form)
        {
            var member = (U_Member)Session["CurrentUser"];
             
            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];
            search.MemberId = member.ID;
            var rList = _scoreService.QueryScoreList(search, view);
            JsonQTable at = JsonQTable.ConvertFromPagedList<U_Score_EX>(rList, colkey, colsinfo.Split(','));
            return Json(at); 
        } 
        #endregion

        #region 我的抽奖记录
        public ActionResult MyLottery()
        {
            return View();
        }
        [HttpPost]
        public ActionResult MyLottery(FormCollection form, T_PrizeSearch search)
        {
            var member = (U_Member)Session["CurrentUser"];
            search.mid = member.ID.ToString();
            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];
            var rList = _scoreService.MyLottery(view, search);
            JsonQTable at = JsonQTable.ConvertFromPagedList<T_Prize>(rList, colkey, colsinfo.Split(','));
            return Json(at);
        }
        #endregion

        #region 签到

       [HttpPost]
        public ActionResult CheckToDay(ScoreSearch search, FormCollection form)
        {
              JsonSMsg msg = new JsonSMsg();
             var member = (U_Member)Session["CurrentUser"];
             var ischeck = _scoreService.Ischeck(member.ID); 
           if (ischeck)
           {
               msg.Data = "";
               msg.Message = "✔您已签到过,请明天再来";
               msg.Status = -1;
               return Json(msg);
           }

           var lastcheck = _scoreService.GetLastCheck(member.ID);
             var check = new U_Check
               {
                   MemberId = member.ID,
                   CheckTime = DateTime.Now
               }; 
           if ( lastcheck!=null && 
            new TimeSpan(((DateTime)lastcheck.CheckTime).AddDays(1).Ticks - ((DateTime)check.CheckTime).Ticks).Days == 0)
           {
               check.CheckDay = lastcheck.CheckDay + 1;
           }
           else
           {
                check.CheckDay = 0;
           }  
            _scoreService.SaveCheck(check);  
            //积分
            _scoreService.SaveScore("签到", member.ID);  
           msg.Data = "";
           msg.Message = "您已成功领取5个积分";
           msg.Status = 1;
           return Json(msg);
        }

       public ActionResult Sign() 
       {
           U_Member m = (U_Member)Session["CurrentUser"];
           ViewBag.isCheck = _scoreService.Ischeck(m.ID); 
           ViewBag.myScore = _scoreService.GetScoreByMID(m.ID);
           return View();
       }

       [HttpGet]
       public ActionResult QuerySignList(int year,int month)
       { 
           U_Member u = (U_Member)Session["CurrentUser"];
           List<U_Check_EX> clist = _scoreService.QueryCheckList(u.ID.ToString(),year,month); 
  
          IList<SignDayList> _signDayList = new List<SignDayList>(); 
           foreach (var c in clist)
           {
               var signDay = new SignDayList
               {
                   signDay = Convert.ToDateTime(c.CheckTime).Day
               }; 
               _signDayList.Add(signDay);
           } 
           return Json(_signDayList, JsonRequestBehavior.AllowGet);
       }
        #endregion
    }
   
}
